using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using ProtectThePie;

namespace ProtectThePieWP7.AdditionalClasses
{
    public enum LeverState
    {
        on,
        off
    }

    public abstract class Lever
    {
        protected Rectangle rectangle;
        protected Vector2 position;
        protected LeverState state;
        protected Sprite sprite;

        //public Lever(bool boolToSwitch, Rectangle rectangle, Texture2D texture)
        //{           
        //    this.rectangle = rectangle;
        //    position = new Vector2(rectangle.X, rectangle.Y);
        //    sprite = new Sprite(texture, new Vector2(rectangle.X, rectangle.Y) , rectangle.Width / 2, 2);
        //    sprite.AddTexel((int)LeverState.on, (int)LeverState.on, 1);
        //    sprite.AddTexel((int)LeverState.off, (int)LeverState.off, 1);           
        //    Switch(boolToSwitch);
        //    sprite.Update(-1.57f);

        //}
        
        public void Update(TouchCollection touches, ref bool boolToSwitch)
        {
            Switch(boolToSwitch);

            foreach (TouchLocation tl in touches)
            {
                if (Vector2.Distance(tl.Position, position) < 50 && tl.State == TouchLocationState.Released)
                {
                    boolToSwitch = !boolToSwitch;
                    Switch(boolToSwitch);
                }
            }
        }

        public bool Update(TouchCollection touches)
        {
            foreach (TouchLocation tl in touches)
            {
                if (Vector2.Distance(tl.Position, position) < 50 && tl.State == TouchLocationState.Released)
                {
                    Switch();
                    return true;
                }
            }
            return false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            sprite.Draw(spriteBatch, (int)state);
        }

        public void Switch(bool boolToSwitch)
        {
            if (boolToSwitch) state = LeverState.on;
            else state = LeverState.off;

            sprite.Update((int)state);
        }

        public void Switch()
        {
            if (state == LeverState.off) state = LeverState.on;
            else state = LeverState.off;

            sprite.Update((int)state);
        }
    }
}
